package com.devsoft.foodfrenzy;

import java.util.ArrayList;
import java.util.Random;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class ArcadeScene extends BaseScene {
	
	private HUD gameHUD;
	private Text scoreText;
	private Text timeText;
	private Text comboText;
	private int score = 0;
	private int high_score;
	private int game_time = 900;
	private PhysicsWorld physicsWorld;
	private Player chef;
	private Pan pan;
	private Pan double_pan;
	private Food food;
	private PowerUp power;
	private int foods_inplay = 0;
	private int max_foods_inplay = 8;
	private boolean power_inplay = false;
	private long base_time;
	private long power_time;
	private int current_food;
	private int current_power;
	public static ArrayList <Food> onScreen = new ArrayList <Food> ();
	private Random generator = new Random();
	private int powerup_state = 0;
	private Timer power_duration = new Timer(8000);
	private boolean game_over = false;
	private int last_drop;
	private int combo_chain = 0;
	private int score_watch =0;
	private int drop_base_food;
	private int drop_base_power;
	private boolean new_high_score = false;
	private AnimatedSprite start_animation;
	private TiledSprite powerup_img;
	private boolean game_start = false;
	private boolean init_flag = false;
	private int multiplier = 1;
	
	@Override
    public void createScene()
    {
		//UserData.getInstance().init(activity.getBaseContext());
		high_score = UserData.getInstance().getHighScore(1);
		createBackground();
	    createHUD();
	    createControllers();
	    createPhysics();

	    if(resourcesManager.main_menu_music.isPlaying())
		{
			resourcesManager.main_menu_music.pause();
			resourcesManager.main_menu_music.seekTo(0);
		}
	    
	    spawnChef();
	    createFood();
	    createPowerUp();
	    updateHUD(); //Reset the HUD values
		final Timer food_drop_timer = new Timer(1800000);
	    food_drop_timer.start();
	    
	    powerup_img = new TiledSprite(400, 32, resourcesManager.poweruphud, vbom);
	    powerup_img.setCurrentTileIndex(0);
	    attachChild(powerup_img); 
	    
	    start_animation = new AnimatedSprite(400, 240, resourcesManager.animation, vbom);
	    start_animation.animate(1000);
	    attachChild(start_animation);
	    if(!(UserData.getInstance().getMusicPref()))
	    {
			resourcesManager.game_countdown.play();
		}
	    
	    drop_base_food = 2000; 
	    drop_base_power = 10000; 
	    this.registerUpdateHandler(new TimerHandler(0.1f, true, new ITimerCallback() //Game timer
	    {                                    
	        public void onTimePassed(final TimerHandler pTimerHandler)
	        {
	            if(game_start)
	            {
	            	game_time--;
	            	updateHUD();
	            	if(game_time == 0){
	            		game_over = true;
	            	}
	            }
	        }
	    }));
		this.registerUpdateHandler(new TimerHandler(0.02f, true, new ITimerCallback() { //MAIN GAME LOOP
            @Override
            public void onTimePassed(TimerHandler pTimerHandler) 
            {
            	updateHUD();
            	
            	if (start_animation.getCurrentTileIndex() == 5){
        	    	start_animation.stopAnimation();
        	        detachChild(start_animation);
        	        game_start = true;
        	    }
            	
            	if(!onScreen.isEmpty())
            		updateFoodOnScreen();
            	
            	if(power_inplay == true)
            		updatePowerUp();
            	
            	if(game_start){
            		
            		if(!(UserData.getInstance().getMusicPref()))
            		{
            			resourcesManager.game_music.play();
            		}
            		
            		if(!init_flag)
            		{
            			base_time = food_drop_timer.getCurrentTime();
            			power_time = food_drop_timer.getCurrentTime();
            			init_flag = true;
            		}
            		
            	    if(((food_drop_timer.getCurrentTime() - base_time) > drop_base_food) && (foods_inplay < max_foods_inplay))
            	    {
            	    	dropFood();
            	    	foods_inplay++;
            	    	base_time = food_drop_timer.getCurrentTime();
            	    	drop_base_food = (generator.nextInt(3) + 2) * 1000;
            	    }
            	
            	    if((food_drop_timer.getCurrentTime() - power_time > drop_base_power) && !power_inplay)
            	    {
            	    	dropPowerUp();
            	    	power_inplay = true;
            	    	power_time = food_drop_timer.getCurrentTime();
            	    	drop_base_power = (generator.nextInt(21) + 15) * 1000;
            	    }
            	}
            	
            	if(score_watch >= 100){
            		game_time += 150;
            		score_watch -= 100;
            	}
            	
            	if(game_over){
            		game_over = false;
            		
            		UserData.getInstance().setHighScore(high_score,1);
            		UserData.getInstance().increaseUnlockVal(2);
            		UserData.getInstance().updateAchievements();
            		UserData.getInstance().setLastScore(score);
            		
            		if(new_high_score == true)
            		{
            			UserData.getInstance().raiseFlag(0);
            		}
            		
            		if(resourcesManager.game_music.isPlaying())
            		{
            			resourcesManager.game_music.pause();
            			resourcesManager.game_music.seekTo(0);
            		}
            		
            		gameHUD.setVisible(false);
            		SceneManager.getInstance().loadGameOverScene(engine);
            	}
            }
        }));
		this.registerUpdateHandler(new TimerHandler(0.4f, true, new ITimerCallback() //Player animation
	    {                                    
	        public void onTimePassed(final TimerHandler pTimerHandler)
	        {
	        	switch(chef.getSprite().getCurrentTileIndex()){
	    		case 0:
	    			chef.getSprite().setCurrentTileIndex(2);
	    			break;
	    		case 1:
	    			chef.getSprite().setCurrentTileIndex(3);
	        		break;
	    		case 2:
	    			chef.getSprite().setCurrentTileIndex(0);
	        		break;
	    		case 3:
	    			chef.getSprite().setCurrentTileIndex(1);
	        		break;
	        	}
	        }
	    }));
    }

	private void createBackground()
	{
		attachChild(new Sprite(400, 240, resourcesManager.game_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	}
	

	@Override
	public void onBackKeyPressed()
	{
		SceneManager.getInstance().loadMenuScene(engine, this.getSceneType());
		if(resourcesManager.game_music.isPlaying())
		{
			resourcesManager.game_music.pause();
			resourcesManager.game_music.seekTo(0);
		}
		
		if(resourcesManager.game_countdown.isPlaying())
		{
			resourcesManager.game_countdown.pause();
			resourcesManager.game_countdown.seekTo(0);
		}
	}
    

    @Override
    public SceneType getSceneType()
    {
        return SceneType.SCENE_ARCADE;
    }
    

    @Override
    public void disposeScene()
    {
        camera.setHUD(null);
        camera.setCenter(400, 240);
        
        this.detachSelf();
        this.dispose();
        // TODO code responsible for disposing scene
        // removing all game scene objects.
    }
    
    
    private void createHUD()
    {
        gameHUD = new HUD();
        
        // CREATE SCORE TEXT
        scoreText = new Text(20, 420, resourcesManager.font, "Score:0123456789()", new TextOptions(HorizontalAlign.LEFT), vbom);
        scoreText.setAnchorCenter(0, 0);
        scoreText.setText("Score: " + this.score + " (" + this.high_score +")");
        
        timeText = new Text(530, 420, resourcesManager.font, "Time:0123456789s", new TextOptions(HorizontalAlign.RIGHT), vbom);
        timeText.setAnchorCenter(0, 0);
        timeText.setText("Time: " + game_time / 10 + " s");
        
        comboText = new Text(20, 380, resourcesManager.small_font, "Combo:0123456789()x", new TextOptions(HorizontalAlign.LEFT), vbom);
        comboText.setAnchorCenter(0, 0);
        comboText.setText("Combo: " + this.combo_chain);
        
        gameHUD.attachChild(scoreText);
        gameHUD.attachChild(timeText);
        gameHUD.attachChild(comboText);
        
        camera.setHUD(gameHUD);
        
    }
    
    
    private void createControllers()
    {
    	if(UserData.getInstance().getControlsPref()){
    	
    	final Rectangle screen = new Rectangle(400, 240, 800, 480, vbom)	//camera at center, rectangle is inverted?
        {
    		int contactPoint = 0;
    		float point_x = 0;
    		
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
            	if (touchEvent.isActionDown())
            	{
            		if (contactPoint == 0)
            			point_x = touchEvent.getX();
            		
            		contactPoint++;
            		
            	}
            		
            	if (touchEvent.isActionUp())
                {
            		contactPoint = 0;
            		
                	if (touchEvent.getX() < point_x && (point_x - touchEvent.getX()) > 15)
                	{
                		//move left
	                	if(chef.getPosition() != 1)
	                	{
	                		chef.getBody().setTransform(chef.getBody().getWorldCenter().x - 7.3f, chef.getBody().getWorldCenter().y, 0.0f);
	                		pan.getBody().setTransform(pan.getBody().getWorldCenter().x - 7.3f, pan.getBody().getWorldCenter().y, 0.0f);
	                		double_pan.getBody().setTransform(double_pan.getBody().getWorldCenter().x - 7.3f, double_pan.getBody().getWorldCenter().y, 0.0f);
	                		chef.setPosition(chef.getPosition() - 1);
	                		if(!(UserData.getInstance().getSoundPref())){
	                        	resourcesManager.game_walk.play();
	                        }
	                		
	                	}
	                	else if(chef.getPosition() == 1 && (point_x - touchEvent.getX()) > 15 && chef.getRotation() == 0){
                			pan.rotate(chef.getRotation());
                			double_pan.rotate(chef.getRotation());
	                    	chef.rotate();
	                    	if(!(UserData.getInstance().getSoundPref())){
	                        	resourcesManager.game_flip.play();
	                        }
	                	}
	                	
                	}
                	else if (touchEvent.getX() > point_x && (touchEvent.getX() - point_x) > 15)
                		{
                			//move right
                			if(chef.getPosition() != 3)
                			{
                        		chef.getBody().setTransform(chef.getBody().getWorldCenter().x + 7.3f, chef.getBody().getWorldCenter().y, 0.0f);
                        		pan.getBody().setTransform(pan.getBody().getWorldCenter().x + 7.3f, pan.getBody().getWorldCenter().y, 0.0f);
                        		double_pan.getBody().setTransform(double_pan.getBody().getWorldCenter().x + 7.3f, double_pan.getBody().getWorldCenter().y, 0.0f);
                        		chef.setPosition(chef.getPosition() + 1);
                        		if(!(UserData.getInstance().getSoundPref())){
                                	resourcesManager.game_walk.play();
                                }
                        		
                        	}
                			else if(chef.getPosition() == 3 && (touchEvent.getX() - point_x) > 15 && chef.getRotation() == 1){
	                			pan.rotate(chef.getRotation());
	                			double_pan.rotate(chef.getRotation());
		                    	chef.rotate();
		                    	if(!(UserData.getInstance().getSoundPref())){
		                        	resourcesManager.game_flip.play();
		                        }
                			}
                			
                		}
                	else
                		{
                			pan.rotate(chef.getRotation());
                			double_pan.rotate(chef.getRotation());
                        	chef.rotate();
                        	if(!(UserData.getInstance().getSoundPref())){
                            	resourcesManager.game_flip.play();
                            }
                		}
                	
                }
                
                return true;
                
            };
        };
        
        screen.setColor(Color.YELLOW);
    	screen.setVisible(false);
        
        gameHUD.registerTouchArea(screen);
        gameHUD.attachChild(screen);
        
        camera.setHUD(gameHUD);
    	}
    	
    	else
    	{
    		final Rectangle left = new Rectangle(75, 240, 150, 480, vbom)
            {
                public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
                {
                    if (touchEvent.isActionUp())
                    {
                    	if(chef.getPosition() != 1){
                    		chef.getBody().setTransform(
                    			chef.getBody().getWorldCenter().x - 7.3f, 
                    			chef.getBody().getWorldCenter().y, 
                    			0.0f);
                    		pan.getBody().setTransform(
                    			pan.getBody().getWorldCenter().x - 7.3f, 
                    			pan.getBody().getWorldCenter().y, 
                    			0.0f);
                    		double_pan.getBody().setTransform(
                    				double_pan.getBody().getWorldCenter().x - 7.3f, 
                    				double_pan.getBody().getWorldCenter().y, 
                        			0.0f);
                    		chef.setPosition(chef.getPosition() - 1);
                    		if(!(UserData.getInstance().getSoundPref())){
                            	resourcesManager.game_walk.play();
                            }
                    	}
                    	else if(chef.getPosition() == 1 && chef.getRotation() == 0){
                			pan.rotate(chef.getRotation());
                			double_pan.rotate(chef.getRotation());
	                    	chef.rotate();
	                    	if(!(UserData.getInstance().getSoundPref())){
                            	resourcesManager.game_flip.play();
                            }
	                	}
                    }
                    return true;
                };
            };
        	left.setColor(Color.YELLOW);
        	left.setVisible(false);
            
            final Rectangle center = new Rectangle(400, 240, 500, 480, vbom)
            {
                public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
                {
                    if (touchEvent.isActionUp())
                    {
                    	pan.rotate(chef.getRotation());
                    	double_pan.rotate(chef.getRotation());
                    	chef.rotate();
                    	if(!(UserData.getInstance().getSoundPref())){
                        	resourcesManager.game_flip.play();
                        }
                    }
                    return true;
                };
            };
            center.setColor(Color.BLUE);
            center.setVisible(false);
            
            final Rectangle right = new Rectangle(725, 240, 150, 480, vbom)
            {
                public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
                {
                    if (touchEvent.isActionUp())
                    {
                    	if(chef.getPosition() != 3){
                    		chef.getBody().setTransform(
                    			chef.getBody().getWorldCenter().x + 7.3f, 
                    			chef.getBody().getWorldCenter().y, 
                    			0.0f);
                    		pan.getBody().setTransform(
                    			pan.getBody().getWorldCenter().x + 7.3f, 
                    			pan.getBody().getWorldCenter().y, 
                    			0.0f);
                    		double_pan.getBody().setTransform(
                    				double_pan.getBody().getWorldCenter().x + 7.3f, 
                    				double_pan.getBody().getWorldCenter().y, 
                        			0.0f);
                    		chef.setPosition(chef.getPosition() + 1);
                    		if(!(UserData.getInstance().getSoundPref())){
                            	resourcesManager.game_walk.play();
                            }
                    	}
                    	else if(chef.getPosition() == 3 && chef.getRotation() == 1){
                			pan.rotate(chef.getRotation());
                			double_pan.rotate(chef.getRotation());
	                    	chef.rotate();
	                    	if(!(UserData.getInstance().getSoundPref())){
                            	resourcesManager.game_flip.play();
                            }
	                	}
                    }
                    return true;
                };
            };
            right.setColor(Color.RED);
            right.setVisible(false);
            
            gameHUD.registerTouchArea(left);
            gameHUD.registerTouchArea(right);
            gameHUD.registerTouchArea(center);
            gameHUD.attachChild(center);
            gameHUD.attachChild(left);
            gameHUD.attachChild(right);
            
            camera.setHUD(gameHUD);
    	}
    }
    
    
    public void addToScore(int i)
    {
    	if ((this.combo_chain >= 25) && (this.combo_chain < 50))
    	{
    		i *= 2;
    		this.multiplier = 2;
    		
    	}
    	else 
    		if (this.combo_chain >= 50 && (this.combo_chain < 75))
    		{
    			i *= 4;
    			this.multiplier = 4;
    			
    		}
    		else
    			if (this.combo_chain >= 100)
    			{
    				i *= 8;
    				this.multiplier = 8;
    				
    			}
    	
        if(this.powerup_state == 1){
        	score += i*2;
        	score_watch += i*2;
        }
        else{
        	score += i;
        	score_watch += i;
        }
        
        if (score >= high_score){
        	high_score = score;
        	new_high_score = true;
        }
        
    }
    
    public void updateHUD()
    {
    	scoreText.setText("Score: " + this.score + " (" + this.high_score +")");
    	timeText.setText("Time: " + game_time / 10 + " s");
    	comboText.setText("Combo: " + this.combo_chain + " (X" + multiplier + ")");
    	if(powerup_state !=0)
    	{
    		if(!power_duration.running())
    		{
    			if(this.powerup_state == 4)
    			{
    				double_pan.getSprite().setVisible(false);
                    double_pan.getBody().setActive(false);
    			}
    			
    			this.powerup_state = 0;
    			powerup_img.setCurrentTileIndex(0);
    			
    		}
    		
    	}
    }
    
    private void createPhysics()
    {
        physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, -3), false);
        physicsWorld.setContactListener(createContactListener());
        registerUpdateHandler(physicsWorld);
        
    }
    
    private void createPlayerBody(Player player){
    	final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
    	objectFixtureDef.filter.groupIndex = -1;
    	
    	player.setBody(PhysicsFactory.createBoxBody(physicsWorld, player.getSprite(), BodyType.StaticBody, objectFixtureDef));
        player.getBody().setUserData("player");
        player.getBody().setFixedRotation(true);
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(player.getSprite(), player.getBody(), true, false));
        
        player.getSprite().setScale(1.5f);
        player.getSprite().setPosition(350, 125);
    	attachChild(player.getSprite());
    }
    
    public void createPanBody(Pan pan){
    	final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
    	objectFixtureDef.filter.groupIndex = 1;
    	
    	pan.setBody(PhysicsFactory.createBoxBody(physicsWorld, pan.getSprite(), BodyType.StaticBody, objectFixtureDef));
        pan.getBody().setUserData("pan");
        pan.getBody().setFixedRotation(true);
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(pan.getSprite(), pan.getBody(), true, false));
        
        pan.getSprite().setScale(1.5f);
        pan.getSprite().setPosition(350, 125);
        if(!pan.getType())
        {
        	pan.getSprite().setVisible(false);
        	pan.getBody().setActive(false);
        }
    	attachChild(pan.getSprite());
    }
    
	private void createFoodBody(Food food)
	{
		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(10.0f, 1.0f, 0.0f);
		objectFixtureDef.filter.groupIndex = -1;
		
		food.setBody(PhysicsFactory.createBoxBody(physicsWorld, food.getSprite(), BodyType.DynamicBody, objectFixtureDef));
		food.getBody().setUserData("food");
		
	    physicsWorld.registerPhysicsConnector(new PhysicsConnector(food.getSprite(), food.getBody(), true, false));
	    
	    food.getSprite().setScale(1.5f);
		attachChild(food.getSprite());
	    
	}
	
	
	private void createPowerUpBody(PowerUp power)
	{
		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
		objectFixtureDef.filter.groupIndex = -1;
		
		power.setBody(PhysicsFactory.createBoxBody(physicsWorld, power.getSprite(), BodyType.DynamicBody, objectFixtureDef));
		power.getBody().setUserData(power.getName());
		power.getBody().applyAngularImpulse(0.5f);
		
	    physicsWorld.registerPhysicsConnector(new PhysicsConnector(power.getSprite(), power.getBody(), true, true));
	    
	    power.getSprite().setScale(1.5f);
    	attachChild(power.getSprite());
	    
	}
    
    
    public void dropFood()
    {
    	int num = generator.nextInt(4);
    	
    	if(num == 0 && last_drop == 3){
    		num++;
    	}
    	if(num == 3 && last_drop == 0){
    		num--;
    	}	
    	
    	last_drop = num;
    	
    	switch(num){
    		case 0:
    			num = 40;
    		break;
    		case 1:
    			num = 280;
    		break;
    		case 2:
    			num = 520;
    		break;
    		case 3:
    			num = 760;
    		break;
    	}
    	
    	current_food = generator.nextInt(18);
    	
    	TiledSprite newsprite =  findFoodSprite(current_food);
    	food = new Food(
    			Food.foodList.get(current_food).getName(), 
    			Food.foodList.get(current_food).getWeight(), 
    			newsprite);
		food.getSprite().setPosition(num, 800);
		createFoodBody(food);
		onScreen.add(food);
		
    }
    
    public void updateFoodOnScreen(){
    	for(int i = 0; i < onScreen.size(); i++){
    		if(onScreen.get(i).getBody().getWorldCenter().y < 0)
    		{    			
    			physicsWorld.destroyBody(onScreen.get(i).getBody());
    			detachChild(onScreen.get(i).getSprite());    			
    			
    			foods_inplay--;
    			combo_chain = 0;
    			multiplier = 1;
    			onScreen.remove(i);
    		}
    		
    		else if(onScreen.get(i).getFullyCooked())
    		{
    			physicsWorld.destroyBody(onScreen.get(i).getBody());
    			detachChild(onScreen.get(i).getSprite());    			
    			
    			foods_inplay--;
    			addToScore(2);
    			onScreen.remove(i);
    		}
    	}
    }
    
    public void updatePowerUp(){
    	if(!power.getSprite().isVisible()){
    		PowerUp temp_power = new PowerUp(
					power.getName(), //Name
					power.getSprite()); //Sprite
    		physicsWorld.destroyBody(power.getBody());
			detachChild(power.getSprite());
			PowerUp.powerList.add(temp_power);
			power_inplay = false;
    	}
    	else if(power.getBody().getWorldCenter().y < 0){
    		PowerUp temp_power = new PowerUp(
					power.getName(), //Name
					power.getSprite()); //Sprite
    		physicsWorld.destroyBody(power.getBody());
			detachChild(power.getSprite());
			PowerUp.powerList.add(temp_power);
			power_inplay = false;
    	}
    	else if(powerup_state !=0){
    		if(!power_duration.running()){
    			if(this.powerup_state == 4)
    			{
    				double_pan.getSprite().setVisible(false);
                    double_pan.getBody().setActive(false);
    			}
    			this.powerup_state = 0;
    			powerup_img.setCurrentTileIndex(0);
    		}
    	}
    }
    
    public void dropPowerUp()
    {
    	int num = generator.nextInt(4);
    	
    	switch(num){
    		case 0:
    			num = 40;
    		break;
    		case 1:
    			num = 280;
    		break;
    		case 2:
    			num = 520;
    		break;
    		case 3:
    			num = 760;
    		break;
    	}
    	
    	current_power = generator.nextInt(10) + 1;
    	
    	if(current_power % 7 == 0){
    		current_power = 2;
    	}
    	else if(current_power % 5 == 0){
    		current_power = 3;
    	}
    	else if(current_power % 4 == 0){
    		current_power = 1;
    	}
    	else if(current_power % 3 == 0){
    		current_power = 0;
    	}
    	else{
    		current_power = 4;
    	}
    	
    	Sprite newsprite =  findPowerSprite(current_power);
    	power = new PowerUp(
    			PowerUp.powerList.get(current_power).getName(), 
    			newsprite);
    	power.getSprite().setPosition(num, 800);
    	createPowerUpBody(power);
    }
    
    public void spawnChef()
    {
    	TiledSprite player_image = new TiledSprite(400, 125, resourcesManager.player, vbom);
        chef = new Player (0,2,player_image);
        
        TiledSprite pan_image = new TiledSprite(290, 65, resourcesManager.pan, vbom);
    	pan = new Pan (0,0, true, pan_image);
    	
    	TiledSprite double_pan_image = new TiledSprite(518, 25, resourcesManager.double_pan, vbom);
    	double_pan = new Pan(0,0, false, double_pan_image);
    	
    	createPlayerBody(chef);
    	createPanBody(pan);
    	createPanBody(double_pan);
    }
    
    private ContactListener createContactListener()
    {
        ContactListener contactListener = new ContactListener()
        {
            @Override
            public void beginContact(Contact contact)
            {
                final Fixture x1 = contact.getFixtureA();
                final Fixture x2 = contact.getFixtureB();

                if (x2.getBody().getUserData().equals("pan") && x1.getBody().getUserData().equals("food") ||
                	x2.getBody().getUserData().equals("food") && x1.getBody().getUserData().equals("pan"))
                {
                    addToScore(1);
                    combo_chain++;
                    if(combo_chain > (UserData.getInstance().getUnlocksVal(6)))
                    {
                    	UserData.getInstance().setUnlocksVal(combo_chain, 6);
                    }
                    if(!(UserData.getInstance().getSoundPref())){
                    	resourcesManager.game_cook.play();
                    }

                    if(x1.getBody().getUserData().equals("food")){
                    	for(int i = 0; i < onScreen.size();i++){
                    		if(x1.getBody().equals(onScreen.get(i).getBody())){
                    			if(powerup_state != 2){
                    				onScreen.get(i).incrementCooked(false);
                    			}
                    			else
                    			{
                    				onScreen.get(i).incrementCooked(true);
                    			}
                    		}
                    	}
                    }
                    if(x2.getBody().getUserData().equals("food")){
                    	for(int i = 0; i < onScreen.size();i++){
                    		if(x2.getBody().equals(onScreen.get(i).getBody())){
                    			if(powerup_state != 2){
                    				onScreen.get(i).incrementCooked(false);
                    			}
                    			else
                    			{
                    				onScreen.get(i).incrementCooked(true);
                    			}
                    		}
                    	}
                    }
                    
                }
                
                
                if (x2.getBody().getUserData().equals("pan") && x1.getBody().getUserData().equals("Score Multiplier") ||
                    	x2.getBody().getUserData().equals("Score Multiplier") && x1.getBody().getUserData().equals("pan"))
                	{
                		powerup_state = 1;
                		power_duration.start();
                		powerup_img.setCurrentTileIndex(1);
                		if(!(UserData.getInstance().getSoundPref())){
                			resourcesManager.game_powerup.play();
                		}
                        if(x1.getBody().getUserData().equals("Score Multiplier") && x1.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                        if(x2.getBody().getUserData().equals("Score Multiplier") && x2.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                    }
                
                if (x2.getBody().getUserData().equals("pan") && x1.getBody().getUserData().equals("Instant Cook") ||
                    	x2.getBody().getUserData().equals("Instant Cook") && x1.getBody().getUserData().equals("pan"))
                	{
                        powerup_state = 2;
                        power_duration.start();
                        powerup_img.setCurrentTileIndex(2);
                        if(!(UserData.getInstance().getSoundPref())){
                        	resourcesManager.game_powerup.play();
                        }
                        if(x1.getBody().getUserData().equals("Instant Cook") && x1.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                        if(x2.getBody().getUserData().equals("Instant Cook") && x2.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                    }
                
                if (x2.getBody().getUserData().equals("pan") && x1.getBody().getUserData().equals("Special") ||
                    	x2.getBody().getUserData().equals("Special") && x1.getBody().getUserData().equals("pan"))
                	{
                		game_time += 150;
                        powerup_state = 3;
                        power_duration.start();
                        powerup_img.setCurrentTileIndex(3);
                        if(!(UserData.getInstance().getSoundPref())){
                        	resourcesManager.game_powerup.play();
                        }
                        if(x1.getBody().getUserData().equals("Special") && x1.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                        if(x2.getBody().getUserData().equals("Special") && x2.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                    }
                
                if (x2.getBody().getUserData().equals("pan") && x1.getBody().getUserData().equals("Double Pan") ||
                    	x2.getBody().getUserData().equals("Double Pan") && x1.getBody().getUserData().equals("pan"))
                	{
                        powerup_state = 4;
                        power_duration.start();
                        double_pan.getSprite().setVisible(true);
                        double_pan.getBody().setActive(true);
                        powerup_img.setCurrentTileIndex(4);
                        if(!(UserData.getInstance().getSoundPref())){
                        	resourcesManager.game_powerup.play();
                        }
                        if(x1.getBody().getUserData().equals("Double Pan") && x1.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                        if(x2.getBody().getUserData().equals("Double Pan") && x2.getBody().equals(power.getBody())){
                        	power.getSprite().setVisible(false);
                        }
                    }

            }

            @Override
            public void endContact(Contact contact)
            {
                   
            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold)
            {
                   
            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse)
            {
                   
            }
        };
        return contactListener;
    }
}
